# # -*- coding: utf-8 -*-
# import django_filters
from .models import Good
#
#
# class GoodsFilter(django_filters.rest_framework.FilterSet):
#     pricemin = django_filters.NumberFilter(field_name='shop_price', lookup_expr='gte')
#     pricemax = django_filters.NumberFilter(field_name='shop_price', lookup_expr='lte')
#
#     # na = django_filters
#     class Meta:
#         model = Good
#         fields = ['pricemin', 'pricemax', 'is_new', 'is_hot']

import django_filters
from django.db.models import Q

from .models import Product


class ProductFilter(django_filters.rest_framework.FilterSet):
    """
    商品的过滤类
    # 这里应对 产品表下面的 商品表进行过滤
    """
    price_min = django_filters.NumberFilter(field_name='min_price', lookup_expr='gte')
    price_max = django_filters.NumberFilter(field_name='min_price', lookup_expr='lte')

    top_category = django_filters.NumberFilter(method='top_category_filter')

    def top_category_filter(self, queryset, name, value):
        # 过滤多级
        return queryset.filter(Q(category_id=value) | Q(category__parent_category_id=value) | Q(
            category__parent_category__parent_category_id=value))

    class Meta:
        model = Product
        fields = ['price_min', 'price_max', 'is_new', 'is_recommend']


class GoodsFilter(django_filters.rest_framework.FilterSet):
    """
    商品的过滤类
    # 这里应对 产品表下面的 商品表进行过滤
    """
    price_min = django_filters.NumberFilter(field_name='sold_price', lookup_expr='gte')
    price_max = django_filters.NumberFilter(field_name='sold_price', lookup_expr='lte')

    class Meta:
        model = Good
        # 机器学习算出
        fields = ['price_min', 'price_max', 'is_hot']
        # fields = ['price_min', 'price_max', 'status']  'is_new'
